﻿using SignalBox.Core.Model.Timetable;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SignalBoxEditor.UI
{
    public partial class frmTrainStops : Form
    {
        private TrainSchedule _trainSchedule;

        private TrainScheduleAction currentAction = null;

        public frmTrainStops(TrainSchedule ts)
        {
            InitializeComponent();

            _trainSchedule = ts;

            trainScheduleActionBindingSource.DataSource = ts.ScheduleActions;
            trainScheduleActionBindingSource.ListChanged += trainScheduleActionBindingSource_ListChanged;
            dataGridView1.SelectionChanged += dataGridView1_SelectionChanged;
        }

        void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {

            if (dataGridView1.SelectedRows.Count == 0) return;

            TrainScheduleAction tsa = (TrainScheduleAction)dataGridView1.SelectedRows[0].DataBoundItem;

            if (tsa == currentAction)
            {
                return;
            }

            if (tsa is EnterLayoutAction)
            {
                ucEnterPanel ep = new ucEnterPanel((EnterLayoutAction)tsa);
                pblContainer.Controls.Clear();
                pblContainer.Controls.Add(ep);
                ep.Dock = DockStyle.Fill;
            }
            else if (tsa is ExitLayoutAction)
            {
                ucExitPanel ep = new ucExitPanel((ExitLayoutAction)tsa);
                pblContainer.Controls.Clear();
                pblContainer.Controls.Add(ep);
                ep.Dock = DockStyle.Fill;
                
            }
            else if (tsa is StopAtLocationAction)
            {
                ucStopAtPanel sap = new ucStopAtPanel((StopAtLocationAction)tsa);
                pblContainer.Controls.Clear();
                pblContainer.Controls.Add(sap);
                sap.Dock = DockStyle.Fill;

            }
            

        }

        void trainScheduleActionBindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            foreach (TrainScheduleAction tsa in _trainSchedule.ScheduleActions)
            {
                tsa.Schedule = _trainSchedule;
            }


            _trainSchedule.SortSchedules();

            dataGridView1.Refresh();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            EnterLayoutAction ela = new EnterLayoutAction();
            AddRow(ela);
        }

        private void AddRow(TrainScheduleAction action)
        {
            trainScheduleActionBindingSource.Add(action);

            _trainSchedule.SortSchedules();

            dataGridView1.Refresh();

            SelectRow(action);
        }

        private void SelectRow(TrainScheduleAction action)
        {
            foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                if (dgvr.DataBoundItem == action)
                {
                    dgvr.Selected             = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.IndexOf(dgvr)].Cells[0];
                }
            }
        }

        private void btnStopAt_Click(object sender, EventArgs e)
        {
            StopAtLocationAction sala = new StopAtLocationAction();
            AddRow(sala);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            ExitLayoutAction ela = new ExitLayoutAction();
            AddRow(ela);
        }
    }
}
